package com.example.tongue.ui.Online.Body;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tongue.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class FinalAndFindDoctor extends Activity implements View.OnClickListener {
    private TextView finResult;
    private Button finalCalculation;
    private ArrayList<String> DataList=new ArrayList<String>();
    private double[] doubles;
    private Map<Double,String> DataMap=new HashMap<Double,String>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finalandfinddoctor);
        finResult=findViewById(R.id.FinResult);
         finalCalculation=findViewById(R.id.FinalCalculation);
            finalCalculation.setOnClickListener(this);





        }




    @Override

    public void onClick(View v) {
            switch (v.getId()){
                case R.id.FinalCalculation:{
                    TheResult();

                }
                break;
            }


    }
    public void TheResult(){
        String path=getExternalCacheDir().getPath();

        File cacheFile=new File(path,"PhysiqueDataFile/PhysiqueData.txt");
        if(cacheFile.exists()){
            try {
                //开启读入字符流
                FileReader reader=new FileReader(cacheFile);
                BufferedReader bufferedReader=new BufferedReader(reader);
                String Line;
                if(bufferedReader!=null) {
                    try {
                        while ((Line = bufferedReader.readLine()) != null) {
                            DataList.add(Line );

                        }
//                        System.out.println(DataList);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                            try {
                                bufferedReader.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                }else {
                    Toast.makeText(FinalAndFindDoctor.this,"问卷填写错误,请重新按顺序填写(字符流不存在)",Toast.LENGTH_SHORT);


                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            CalculateResult();
            //最终体质结果
            finResult.setText("最终结果:"+PhysicalJudgment());


        }else {
            Toast.makeText(FinalAndFindDoctor.this,"问卷填写错误,请重新按顺序填写(缓存文件不存在)",Toast.LENGTH_SHORT);
        }




    }
    public void CalculateResult(){
        //将数据转化为double[]类型
        doubles=new double[DataList.size()];

        for (int i=0;i<doubles.length;i++){
            doubles[i]=Double.parseDouble(DataList.get(i));
        }
        DataMap.put(doubles[0],"平和质");
        DataMap.put(doubles[1],"气虚质");
        DataMap.put(doubles[2],"阳虚质");
        DataMap.put(doubles[3],"痰湿质");
        DataMap.put(doubles[4],"湿热质");
        DataMap.put(doubles[5],"血瘀质");
        DataMap.put(doubles[6],"特禀质");
        DataMap.put(doubles[7],"气郁质");
        DataMap.put(doubles[8],"阴虚质");






    }
    public String PhysicalJudgment(){

         double[] OtherDoubles=new double[DataList.size()-1];
        for (int i=1;i<= doubles.length-1;i++){
            OtherDoubles[i-1]=Double.parseDouble(DataList.get(i));
        }
         Arrays.sort(OtherDoubles);


        if(doubles[0]>=60&&OtherDoubles[OtherDoubles.length-1]<40){


            return "平和质";
        }else {
            if (doubles[0]>=60&&OtherDoubles[OtherDoubles.length-1]>=40){
               return DataMap.get(OtherDoubles[OtherDoubles.length-1]);
            }else {
                if(doubles[0]<60&&OtherDoubles[OtherDoubles.length-1]>=40){
                    return DataMap.get(OtherDoubles[OtherDoubles.length-1]);
                }else {
                    //TODO 体质判断没有标准
                    return "倾向于平和质";

                }


            }


        }
    }


}
